package com.neusoft.elmboot.service.Impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.neusoft.elmboot.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.neusoft.elmboot.mapper.WalletMapper;
import com.neusoft.elmboot.po.Wallet;
import com.neusoft.elmboot.service.WalletService;

@Service
public class WalletServiceImpl implements WalletService {


    @Autowired
    private WalletMapper walletMapper;

    @Override
    public int saveWallet(String userId) {
        return walletMapper.saveWallet(userId);
    }

    @Override
    public Wallet getWalletByUserid(String userId) {
        return walletMapper.getWalletByUserid(userId);
    }

    @Override
    public int updateMoney(String userId, BigDecimal account) {
        return walletMapper.updateMoney(userId, account);
    }

    @Override
    public int pay(String userId,BigDecimal amount,Integer type){
        BigDecimal elmMoney = getWalletByUserid("elm").getMoney();
        Wallet wallet = getWalletByUserid(userId);
        int now = updateMoney(userId,wallet.pay(amount)) + updateMoney("elm",elmMoney.add(amount));
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        int a = walletMapper.saveTransaction(formatter.format(date),amount,type,"elm",userId);
        return now;
    }

}
